export function AxValidator(rule, value, callback) {
  try {
    if (rule.required) {

      if (value === null || value === undefined || value === '' || value.length === 0) {
        throw new Error(`请填写${rule.label}！`);
      }
      else if(typeof(value)=='string' && value.trim() == ''){
        throw new Error(`请填写${rule.label}！`);
      }
    }
    if (rule.len && value) {
      if (value.length !== rule.len) {
        throw new Error(`${rule.label}字符数须为${rule.len}`);
      }
    }
    if (rule.maxLength && value) {
      if (value.length > rule.maxLength) {
        throw new Error(`${rule.label}字符数不能大于${rule.maxLength}`);
      }
    }
    if (rule.minLength && value) {
      if (value.length < rule.minLength) {
        throw new Error(`${rule.label}字符数不能小于${rule.minLength}`);
      }
    }
    if ((rule.max || rule.max === 0) && value) {
      if (parseFloat(value) > rule.max) {
        throw new Error(`${rule.label}数值不能大于${rule.max}`);
      }
    }
    if ((rule.min || rule.min === 0) && value) {
      if (parseFloat(value) < rule.min) {
        throw new Error(`${rule.label}数值不能小于${rule.min}`);
      }
    }
    if (rule.pattern && rule.pattern.reg && value) {
      if (!new RegExp(rule.pattern.reg).test(value)) {
        if (rule.pattern.message) {
          throw new Error(rule.pattern.message);
        }
        throw new Error(`${rule.label}格式不正确！`);
      }
    }
    callback(); // 通过验证
  } catch (err) {
    callback(err);
  }
}
